package com.xiexin.web.controller.group;

import com.alibaba.fastjson.JSON;
import com.xiexin.common.annotation.Log;
import com.xiexin.common.core.controller.BaseController;
import com.xiexin.common.core.domain.AjaxResult;
import com.xiexin.common.core.page.TableDataInfo;
import com.xiexin.common.enums.BusinessType;
import com.xiexin.common.utils.poi.ExcelUtil;
import com.xiexin.group.domain.ItemGroup;
import com.xiexin.group.service.IItemGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 产品组的管理Controller
 *
 * @author xiexin
 * @date 2022-03-21
 */
@RestController
@RequestMapping("/group/group")
public class ItemGroupController extends BaseController {
    @Autowired
    private IItemGroupService itemGroupService;

    /**
     * 修改项目组
     */
    @PreAuthorize("@ss.hasPermi('itemGroup:group:edit')")
    @Log(title = "项目组", businessType = BusinessType.UPDATE)
    @PostMapping("/updateGroupAndItemIds")
    public AjaxResult updateGroupAndItemIds(@RequestBody Map map) {
        ItemGroup itemGroup = JSON.parseObject(JSON.toJSONString(map.get("form")), ItemGroup.class);
        List<Integer> itemIds = JSON.parseObject(JSON.toJSONString(map.get("itemIds")), List.class);
        return toAjax(itemGroupService.updateGroupAndItemIds(itemGroup, itemIds));
    }


    @PreAuthorize("@ss.hasPermi('itemGroup:group:add')")
    @PostMapping("/addItemGroupAndItems")
    public AjaxResult addItemGroupAndItems(@RequestBody Map data) {
        List itemIds = JSON.parseObject(data.get("itemIds") + "", List.class);
        ItemGroup itemGroup = JSON.parseObject(JSON.toJSONString(data.get("itemGroup")), ItemGroup.class);
        return toAjax(itemGroupService.addItemGroupAndItems(itemIds, itemGroup));
    }

    /**
     * 查询产品组的管理列表
     */
    @PreAuthorize("@ss.hasPermi('group:group:list')")
    @GetMapping("/list")
    public TableDataInfo list(ItemGroup itemGroup) {
        startPage();
        List<ItemGroup> list = itemGroupService.selectItemGroupList(itemGroup);
        return getDataTable(list);
    }

    /**
     * 导出产品组的管理列表
     */
    @PreAuthorize("@ss.hasPermi('group:group:export')")
    @Log(title = "产品组的管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ItemGroup itemGroup) {
        List<ItemGroup> list = itemGroupService.selectItemGroupList(itemGroup);
        ExcelUtil<ItemGroup> util = new ExcelUtil<ItemGroup>(ItemGroup.class);
        util.exportExcel(response, list, "产品组的管理数据");
    }

    /**
     * 获取产品组的管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('group:group:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(itemGroupService.selectItemGroupById(id));
    }

    /**
     * 新增产品组的管理
     */
    @PreAuthorize("@ss.hasPermi('group:group:add')")
    @Log(title = "产品组的管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ItemGroup itemGroup) {
        return toAjax(itemGroupService.insertItemGroup(itemGroup));
    }

    /**
     * 修改产品组的管理
     */
    @PreAuthorize("@ss.hasPermi('group:group:edit')")
    @Log(title = "产品组的管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ItemGroup itemGroup) {
        return toAjax(itemGroupService.updateItemGroup(itemGroup));
    }

    /**
     * 删除产品组的管理
     */
    @PreAuthorize("@ss.hasPermi('group:group:remove')")
    @Log(title = "产品组的管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        System.out.println("这里 执行了 删除");
        // 根据  ids  for循环 删除 中间表种的数据

        return toAjax(itemGroupService.deleteItemGroupByIds(ids));
    }
}
